package com.auto.task;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.auto.MonitorUtil;
import com.auto.upgrade.utils.AutoUtil;
import com.system.comm.utils.FrameFileUtil;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameMapUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * 程序升级服务
 * @author yuejing
 * @date 2019年12月1日 上午9:21:10
 * @version V1.0.0
 */
public class AutoUpgradeTask {

	private static final Logger LOGGER = LoggerFactory.getLogger(AutoUpgradeTask.class);

	/**
	 * 启动服务
	 * @param initialDelay	首次执行的延时时间
	 * @param period		定时执行的间隔时间
	 */
	public void run(int initialDelay, int period) {
		LOGGER.info("初始化 - 程序升级服务的定时任务.");
		ScheduledExecutorService service = Executors.newScheduledThreadPool(20);
		//线程，每隔5秒调用一次
		Runnable runnable = new Runnable() {
			public void run() {
				Map<String, Object> upgradeMap = AutoUtil.readUpgradeInfo();
				Integer open = FrameMapUtil.getInteger(upgradeMap, "open");
				if (open == null || open == 0) {
					return;
				}
				String username = FrameMapUtil.getString(upgradeMap, "username");
				List<Map<String, Object>> projects = FrameMapUtil.getListMap(upgradeMap, "projects");
				for (Map<String, Object> map : projects) {
					String packagepath = FrameMapUtil.getString(map, "packagepath");
					String packagename = FrameMapUtil.getString(map, "packagename");
					LOGGER.info("扫描部署版本包的路径: {}", packagepath);
					Map<String, List<String>> fileMap = FrameFileUtil.getDirFile(packagepath, false);
					List<String> fileList = FrameMapUtil.getListString(fileMap, "files");
					if (fileList != null && fileList.size() > 0) {
						String filePath = null;
						for (String path : fileList) {
							if (path.endsWith(packagename)) {
								filePath = path;
								break;
							}
						}
						if (filePath == null) {
							continue;
						}
						LOGGER.info("执行 - 程序升级服务.");
						ResponseFrame uploadFrame = MonitorUtil.sysFileUpload(filePath, 10);
						if (ResponseCode.SUCC.getCode() == uploadFrame.getCode()) {
							Map<String, Object> resultMap = uploadFrame.getBody();
							String url = FrameMapUtil.getString(resultMap, "url");
							String appcode = FrameMapUtil.getString(map, "appcode");
							String version = FrameMapUtil.getString(map, "version");
							String remark = FrameMapUtil.getString(map, "remark");
							String logpath = FrameMapUtil.getString(map, "logpath");
							String clients = FrameMapUtil.getString(map, "clients");
							ResponseFrame deployFrame = MonitorUtil.prjVersionDeploy(appcode, username, version, remark, clients, logpath, url);
							if (ResponseCode.SUCC.getCode() == deployFrame.getCode()) {
								LOGGER.info("部署项目成功: {}", FrameJsonUtil.toString(deployFrame));
								// 删除本地部署后的jar
								FrameFileUtil.deleteFile(filePath);
							} else {
								LOGGER.info("部署项目失败: {}", FrameJsonUtil.toString(deployFrame));
							}
						} else {
							LOGGER.info("上传文件失败: {}", FrameJsonUtil.toString(uploadFrame));
						}
					}
				}
			}
		};
		// 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间  
		service.scheduleAtFixedRate(runnable, initialDelay, period, TimeUnit.SECONDS);
	}
}
